package com.f6.weixin.houtai.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.f6.exceptions.BusinessException;
import com.f6.service.CommonService;
import com.f6.utils.F6SystemUtils;
import com.f6.utils.F6WebUtil;
import com.f6.utils.SystemConstans;
import com.f6.vo.DBParameter;
import com.f6.weixin.vo.DicVO;
import com.f6.weixin.vo.Dish;
import com.f6.weixin.vo.DishVO;

@Controller
@RequestMapping(SystemConstans.HOUTAI + "/dish")
public class ProductController {
	private Logger logger = LoggerFactory.getLogger(ProductController.class);

	@Autowired
	private CommonService commonservice;

	@RequestMapping(value = "list", method = RequestMethod.GET, produces = "application/text;charset=UTF-8")
	public ModelAndView dishlist() throws BusinessException {
		logger.info("enter dishlist");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("ht/dishlist");

		Map paramap = new HashMap<String, String>();
		DBParameter dbparam = new DBParameter();
		dbparam.setAction("selectDishList");
		dbparam.setModule("putong.Dish");
		dbparam.setParameter(paramap);

		List<Map<String, ?>> list = commonservice.queryMore(dbparam);
		// List<Dish> list = new ArrayList<Dish>();
		logger.info("获取列表:" + list.size());
		mv.addObject("dishlist", list);
		return mv;
	}

	@RequestMapping(value = "edit", method = RequestMethod.GET)
	public ModelAndView dishlist(HttpServletRequest request) throws IOException, BusinessException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("ht/dishedit");

		Map paramap = new HashMap<String, String>();
		DBParameter dbparam = new DBParameter();
		dbparam.setAction("selectByPrimaryKey");
		dbparam.setModule("putong.Dish");

		String dishCode = request.getParameter("dishCode");
		Map param = new HashMap();
		param.put("dishCode", dishCode);
		dbparam.setParameter(param);
		Map dishMap = commonservice.queryOne(dbparam);
		DishVO vo = (DishVO) F6SystemUtils.parseMap2Obj(dishMap, DishVO.class.getName());

		mv.addObject("dish", vo);
		return mv;
	}

	@RequestMapping(value = "update", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map saveUpdate(HttpServletRequest request, @RequestBody Map param) throws IOException, BusinessException {

		DBParameter dbparam = new DBParameter();
		dbparam.setAction("updateByPrimaryKeySelective");
		dbparam.setModule("putong.Dish");
		dbparam.setParameter(param);
		commonservice.change(dbparam, SystemConstans.CHANGE_ACTION_UPDATE);
		logger.info("+++++++++++++++++++++updated successful+++++++++++++++++++++++++++");

		return F6WebUtil.buildResponseMap(SystemConstans.RESPONSE_LABEL_SUCCESS, "", "修改成功");

	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public ModelAndView create(HttpServletRequest request) throws IOException, BusinessException {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("ht/dishnew");
		DBParameter dbparam = new DBParameter();
		dbparam.setAction("selectByCategory");
		dbparam.setModule("putong.Dic");
		Map<String, Object> param = new HashMap();
		param.put("dicCategory", "dish_category");
		dbparam.setParameter(param);
		List<Map<String, ?>> resultmap = commonservice.queryMore(dbparam);
		List<DicVO> list = new ArrayList<DicVO>();
		for (Map<String, ?> map : resultmap) {
			DicVO vo = (DicVO) F6SystemUtils.parseMap2Obj(map, "com.f6.weixin.vo.DicVO");

			list.add(vo);

		}
		Dish dish = null;
		mv.addObject("dishgroup", list);
		mv.addObject("dish", dish);
		return mv;
	}

	@RequestMapping(value = "save", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map save(HttpServletRequest request, @RequestBody Map param) throws BusinessException {

		DBParameter dbparam = new DBParameter();
		dbparam.setAction("insert");
		dbparam.setModule("putong.Dish");

		dbparam.setParameter(param);
		commonservice.change(dbparam, SystemConstans.CHANGE_ACTION_INSERT);
		logger.info("+++++++++++++++++++++save successful+++++++++++++++++++++++++++");
		// ModelAndView mv = new ModelAndView();
		// mv.setViewName("ht/dishlist");
		// Dish dish = null;
		// mv.addObject("dish", dish);
		// mv
		return F6WebUtil.buildResponseMap(SystemConstans.RESPONSE_LABEL_SUCCESS, "", "创建成功");
	}

	@RequestMapping(value = "changestatus", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map changestatus(HttpServletRequest request, @RequestBody Map param) throws BusinessException {
		String key = (String) param.get("key");
		String value = (String) param.get("value");
		DBParameter dbparam = new DBParameter();
		dbparam.setAction("updateByPrimaryKeySelective");
		dbparam.setModule("putong.Dish");
		param.remove("key");
		param.put(key, value);
		dbparam.setParameter(param);
		commonservice.change(dbparam, SystemConstans.CHANGE_ACTION_UPDATE);
		logger.info("+++++++++++++++++++++updated successful+++++++++++++++++++++++++++");

		return F6WebUtil.buildResponseMap(SystemConstans.RESPONSE_LABEL_SUCCESS, "", "修改成功");
	}

}
